package com.hebeu.device.controller;

import com.hebeu.device.pojo.*;
import com.hebeu.device.service.PartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Part;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/part")
public class PartController {
    private PartService partService;

    @Autowired
    public PartController(PartService partService) {
        this.partService = partService;
    }

    /**
     * 查询备件列表
     *
     * @return
     */
    @GetMapping("/partList")
    public List<DPart> getPartList() {
        List<DPart> dParts = partService.selectAllDparts();
        return dParts;
    }

    @RequestMapping("/addPart")
    public void addPart(DPart dPart) {
        partService.addPart(dPart);
        partService.addPartFluid(dPart);
    }

    /**
     * 获取备件信息
     *
     * @param partId
     * @return
     */
    @PostMapping("/getPartInfo")
    public Object getPartInfo(Integer partId) {
        // 创建map，用于返回多种数据
        Map<String, Object> infoMap = new HashMap<>();
        DPart part = partService.getPartById(partId);
        infoMap.put("part", part);
        return infoMap;
    }

    /**
     * 出库
     *
     * @return
     */
    @RequestMapping("/outPart")
    public void updatePart(DPart dPart, Integer outNumber) {
        int remain = dPart.getNumber() - outNumber;
        dPart.setNumber(remain);
        partService.updatePart(dPart);
        partService.addPartFluid(dPart);
    }
}
